package com.github.leecho.ssc.captcha;

import java.util.Random;

/**
 * @author LIQIU
 * created on 2020/11/13
 **/
public class RandomCaptcha implements Captcha {

    public static final String PRESET_CHARS = "0123456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    private final String value;

    public RandomCaptcha(int size) {
        String chars = PRESET_CHARS;
        int length = chars.length();
        Random random = new Random(System.currentTimeMillis());
        StringBuilder captchaBuilder = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            captchaBuilder.append(chars.charAt(random.nextInt(length - 1)));
        }
        value = captchaBuilder.toString();
    }

    @Override
    public String getValue() {
        return value;
    }
}
